package com.chilli.cosima.action.usermanager;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.TOperator;
import com.chilli.cosima.data.bean.TRole;
import com.chilli.cosima.service.UserService;
import com.chilli.cosima.utils.MD5Encrypt;
import com.opensymphony.xwork2.ActionContext;

public class UserAddAction extends BaseSupport
{
	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 4663886657657933847L;

	private UserService userService;

	private TOperator user;

	private List<TRole> roles;

	public void checkUser() throws IOException
	{
		ActionContext ctx = ActionContext.getContext();

		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		OutputStream out = response.getOutputStream();

		String username = request.getParameter("value");
		if (username != null)
		{
			TOperator operator = userService.findUserByName(username);
			if (operator != null)
			{
				out.write(getText("user.hasregister").getBytes("UTF-8"));
			} else
			{
				out.write("success".getBytes("UTF-8"));
			}

		} else
		{
			out.write(getText("user.hasregister").getBytes("UTF-8"));
		}
	}

	public String execute()
	{
		try
		{
			user.setLoginpwd(MD5Encrypt.encode(user.getLoginpwd()));
			user.setRolename(userService.findRoleNameById(user.getRoleid()));
			userService.createUser(user);
			return SUCCESS;
		} catch (BussinessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{

		}
		return JUMP_PAGE;
	}

	public String jumpPage()
	{
		roles = userService.findAllRoles();
		return JUMP_PAGE;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public TOperator getUser()
	{
		return user;
	}

	public void setUser(TOperator user)
	{
		this.user = user;
	}

	public List<TRole> getRoles()
	{
		return roles;
	}

	public void setRoles(List<TRole> roles)
	{
		this.roles = roles;
	}

}
